package com.youlu.campus.service.wechat.utils;

import com.youlu.campus.common.utils.SM2CryptUtils;
import com.youlu.campus.entity.WechatMerchantConfig;
import org.apache.commons.lang3.StringUtils;

/**
 * @Description: WechatMerchantConfigUtil
 * @Date 2025/6/19
 * @author: su
 */
public class WechatMerchantConfigUtil {

    private static final String privateKey = "00ee4fcf4a4cfb2c377792c1079f72f31347a38d8839a41d19172eacc08a3d30c2";

    public static final String publicKey = "04312380b932557a8888ccf562b3da21387725ef43fd443d706473b7ed8e7fd0e17d0d0f8ebb889bf1a01551799be4aba50d18a41a50411c29ecc222da2842cc64";

    /**
     * 字段 解密
     */

    public static WechatMerchantConfig decrypt(WechatMerchantConfig config) {
        if (StringUtils.isNotBlank(config.getMerchantId())) {
            String merchantId = SM2CryptUtils.decrypt(config.getMerchantId(), privateKey);
            config.setMerchantId(merchantId);
        }
        if (StringUtils.isNotBlank(config.getSerialNumber())) {
            String serialNumber = SM2CryptUtils.decrypt(config.getSerialNumber(), privateKey);
            config.setSerialNumber(serialNumber);
        }
        if (StringUtils.isNotBlank(config.getPlatformSerialNumber())) {
            String platformSerialNumber = SM2CryptUtils.decrypt(config.getPlatformSerialNumber(), privateKey);
            config.setPlatformSerialNumber(platformSerialNumber);
        }
        if (StringUtils.isNotBlank(config.getPathPlatformCertificate())) {
            String pathPlatformCertificate = SM2CryptUtils.decrypt(config.getPathPlatformCertificate(), privateKey);
            config.setPathPlatformCertificate(pathPlatformCertificate);
        }
        if (StringUtils.isNotBlank(config.getPathPrivateKey())) {
            String pathPrivateKey = SM2CryptUtils.decrypt(config.getPathPrivateKey(), privateKey);
            config.setPathPrivateKey(pathPrivateKey);
        }
        if (StringUtils.isNotBlank(config.getPathPublicKey())) {
            String pathPublicKey = SM2CryptUtils.decrypt(config.getPathPublicKey(), privateKey);
            config.setPathPublicKey(pathPublicKey);
        }
        if (StringUtils.isNotBlank(config.getCc())) {
            String cc = SM2CryptUtils.decrypt(config.getCc(), privateKey);
            config.setCc(cc);
        }
        if (StringUtils.isNotBlank(config.getPublicKeyId())) {
            String publicKeyId = SM2CryptUtils.decrypt(config.getPublicKeyId(), privateKey);
            config.setPublicKeyId(publicKeyId);
        }
        if (StringUtils.isNotBlank(config.getApiV3Key())) {
            String apiV3Key = SM2CryptUtils.decrypt(config.getApiV3Key(), privateKey);
            config.setApiV3Key(apiV3Key);
        }
        if (StringUtils.isNotBlank(config.getDesc())) {
            String desc = SM2CryptUtils.decrypt(config.getDesc(), privateKey);
            config.setDesc(desc);
        }
        config.setEncrypt(false);

        return config;
    }

    /**
     * 字段 加密
     */
    public static WechatMerchantConfig encrypt(WechatMerchantConfig config) {
        if (StringUtils.isNotBlank(config.getMerchantId())) {
            String merchantId = SM2CryptUtils.encrypt(config.getMerchantId(), publicKey);
            config.setMerchantId(merchantId);
        }
        if (StringUtils.isNotBlank(config.getSerialNumber())) {
            String serialNumber = SM2CryptUtils.encrypt(config.getSerialNumber(), publicKey);
            config.setSerialNumber(serialNumber);
        }
        if (StringUtils.isNotBlank(config.getPlatformSerialNumber())) {
            String platformSerialNumber = SM2CryptUtils.encrypt(config.getPlatformSerialNumber(), publicKey);
            config.setPlatformSerialNumber(platformSerialNumber);
        }
        if (StringUtils.isNotBlank(config.getPathPlatformCertificate())) {
            String pathPlatformCertificate = SM2CryptUtils.encrypt(config.getPathPlatformCertificate(), publicKey);
            config.setPathPlatformCertificate(pathPlatformCertificate);
        }
        if (StringUtils.isNotBlank(config.getPathPrivateKey())) {
            String pathPrivateKey = SM2CryptUtils.encrypt(config.getPathPrivateKey(), publicKey);
            config.setPathPrivateKey(pathPrivateKey);
        }
        if (StringUtils.isNotBlank(config.getPathPublicKey())) {
            String pathPublicKey = SM2CryptUtils.encrypt(config.getPathPublicKey(), publicKey);
            config.setPathPublicKey(pathPublicKey);
        }
        if (StringUtils.isNotBlank(config.getCc())) {
            String cc = SM2CryptUtils.encrypt(config.getCc(), publicKey);
            config.setCc(cc);
        }
        if (StringUtils.isNotBlank(config.getPublicKeyId())) {
            String publicKeyId = SM2CryptUtils.encrypt(config.getPublicKeyId(), publicKey);
            config.setPublicKeyId(publicKeyId);
        }
        if (StringUtils.isNotBlank(config.getApiV3Key())) {
            String apiV3Key = SM2CryptUtils.encrypt(config.getApiV3Key(), publicKey);
            config.setApiV3Key(apiV3Key);
        }
        if (StringUtils.isNotBlank(config.getDesc())) {
            String desc = SM2CryptUtils.encrypt(config.getDesc(), publicKey);
            config.setDesc(desc);
        }
        config.setEncrypt(true);
        return config;
    }

    public static String encrypt(String string) {
        if (StringUtils.isBlank(string)) {
            throw new NullPointerException("参数不能为null");
        }
        return SM2CryptUtils.encrypt(string, publicKey);
    }
}
